package com.lb.trac.controller.dataloader;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;

import org.json.JSONArray;

public class DataLoaderUtil {

	public static final String TABLE_FIELDS = "tableFields";
	public static final String COLUMNS_FIELD = "tableColumns";
	public static final String DATA = "dataTable";
	

	public static JSONArray fieldsToJson(HttpServletRequest request,
			String[] columns) {
		JSONArray json = new JSONArray();
		for (int i = 0; i < columns.length; i++) {
			String string = columns[i];

			json.put(string);
		}
		request.setAttribute(DataLoaderUtil.TABLE_FIELDS, json.toString());
		return json;
	}

	public static JSONArray dataToJson(HttpServletRequest request,Comparator<String> comparator, 
			String[] columns, Map<String, String> propertiesMapper,
			Object[] objs) throws IllegalAccessException,
			InvocationTargetException, NoSuchMethodException {
		List<Map> mapList = new ArrayList<Map>(); 
		for (int i = 0; i < objs.length; i++) {
			Object obj = objs[i];
			SortedMap<String,Object> map = new TreeMap<String,Object>(comparator);
			for (int j = 0; j < columns.length; j++) {
				String c = columns[j];
				String propertyName = propertiesMapper.get(c);
				Object value = org.apache.commons.beanutils.BeanUtils
						.getProperty(obj, propertyName);
				map.put(c, value);
			}
			
			mapList.add(map);
			System.out.println("DataLoaderUtil.dataToJson() --> "+map);
		}
		String jsonString = convertToJson(mapList,columns);
		System.out.println(jsonString);
		request.setAttribute(DATA, jsonString);
		return null;
	}

	public static JSONArray columnsToJson(HttpServletRequest request,
			String[] headers, String[] columns) {
		JSONArray jsonArray = new JSONArray();
		for (int i = 0; i < columns.length; i++) {
			Map map = new HashMap();
			map.put("header", headers[i]);
			map.put("dataIndex", columns[i]);
			map.put("width", 200);
			jsonArray.put(map);
		}
		request.setAttribute(COLUMNS_FIELD, jsonArray.toString());
		return jsonArray;
	}
	
	private static String convertToJson(List<Map> array,String[] columns){
		String opGraffa = "{";
		String clGraffa = "}";
		String opQuadra="[";
		String clQuadra="]";
		StringBuffer result = new StringBuffer(opQuadra);
		System.out.println("DataLoaderUtil.convertToJson() --> "+array);
		for (Iterator iterator = array.iterator(); iterator.hasNext();) {
			Map map = (Map) iterator.next();
			System.out.println("DataLoaderUtil.convertToJson() --> "+map);
			result.append(opGraffa);
			
			for (int i = 0; i < columns.length; i++) {
				String c = columns[i];
				
				result.append("\"").append(c).append("\":").append("\"").append(map.get(c)).append("\"").append(",");
				
			}
			result = new StringBuffer(result.substring(0, result.lastIndexOf(",")));

			result.append(clGraffa).append(",");
		}
		result = new StringBuffer(result.substring(0, result.lastIndexOf(",")));
		result.append(clQuadra);
		return result.toString();
	}

}
